package com.chiaching.chaoxingsecondkill.controller;

import com.chiaching.chaoxingsecondkill.ChaoxingSecondkillApplication;
import com.chiaching.chaoxingsecondkill.hander.SecondKillHandler;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author jiajing.he
 * @date 2021/3/17 23:24
 */
@RestController
@RequestMapping("user/config")
public class ConfigController {

    @Autowired
    private SecondKillHandler secondKillHandler;
    @Autowired
    private ChaoxingSecondkillApplication chaoxingSecondkillApplication;

    @RequestMapping("setCookies")
    public String setCookies(String phone, String cookies) {
        if (StringUtils.isBlank(cookies)){
            return "请设置cookies!";
        }
        /*if (StringUtils.isBlank(phone)){
            SecondKillHandler.USER_CONFIG.getList().stream().forEach(e -> {
                if (e.getPhone().equals("18973839925")) {
                    e.setCookies(cookies);
                }
            });
        }else*/ if (SecondKillHandler.USER_CONFIG.getList().stream().anyMatch(e -> e.getPhone().equals(phone))) {
            SecondKillHandler.USER_CONFIG.getList().stream().forEach(e -> {
                if (e.getPhone().equals(phone)) {
                    e.setCookies(cookies);
                }
            });
            return "设置成功!";
        }
        return "请输入正确手机号,没有对应用户!";
    }

    @RequestMapping("start")
    public void start() throws Exception {
        chaoxingSecondkillApplication.scheduled();
    }

}
